<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

</head>
<style>
    
    #app{
        width: 220px;
        margin: 100px;
    }
    span{
        border-bottom: 1px solid #000;
        width: 100px;
        display: inline-block;
        text-align: center;
    }
    input{
        outline: 0;
        border: 0;
        width: 100px;
        font-size: 16px;
        text-align: center;
        border-bottom: 1px solid #333;
    }
</style>

<body>
    <div id="app">
        <fieldset>
            <legend>edit练习</legend>
            <p v-for="(v,k,i) in userInfo">
                <label for="">{{k}}</label>:
                <input type="text" v-model="val" ref="ipt" @blur="esc(k)" v-if="isEdit&&i===selectedIndex">
                <span @dblclick="edit(v,i)" v-else>{{v}}</span>
            </p>
        </fieldset>
    </div>


    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                userInfo: {
                    "姓名": "马冬梅",
                    "年龄": 28,
                    "爱好": "打篮球",
                    "身高": "170cm",
                },
                val:"打篮球",
                isEdit:false,
                selectedIndex:""
            },
            methods:{
                edit(v,i){
                    this.isEdit=true;
                    this.selectedIndex=i;
                    this.val=v;
                    this.$nextTick(()=>{
                        console.log(this.$refs.ipt[0].focus());
                    })
                },
                esc(k){
                    this.isEdit=false;
                    console.log(k);
                    this.userInfo[k]=this.val
                }
            }



        })




    </script>
</body>

</html>